//
//  SQLiteManager.m
//  词典
//
//  Created by Justsoso on 16/4/13.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "SQLiteManager.h"
#import <sqlite3.h>

@interface SQLiteManager()
{
    sqlite3 *db;
}
@end

@implementation SQLiteManager
SingletonM(SQLiteManager)

- (void)openDB:(NSString *)dbName
{
    NSString *dbPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:dbName];
    NSLog(@"%@",dbPath);
    
    if (sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)
    {
        NSLog(@"数据库打开成功");
    }
    else
    {
        NSLog(@"数据库打开失败");
    }
}

- (NSString *)findWord:(NSString *)word
{
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Words WHERE word = '%@';",word];
    NSLog(@"%@",sql);
    
    sqlite3_stmt *stmt;
    
//    NSMutableArray *wordList = [NSMutableArray array];
    
    NSString *explain;
    if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK)
    {
        while (sqlite3_step(stmt) == SQLITE_ROW)
        {
            char *wordExplain = sqlite3_column_text(stmt, 1);
            
            explain = [[NSString alloc] initWithUTF8String:wordExplain];
            NSLog(@"%@",explain);
//            NSDictionary *dict = @{@"explain":explain};
//            
//            [wordList addObject:dict];
        }
    }
    return explain;
}

@end
